<div class="layui-card">
    <div class="layui-card-header">
        <h2 class="header-title">教务管理</h2>
        <span class="layui-breadcrumb pull-right">
          <a href="#!console">首页</a>
          <a><cite>教务管理</cite></a>
        </span>
    </div>
    <div class="layui-card-body">
        <div class="layui-form toolbar">
            搜索：
            <select id="edu-search-key">
                <option value="">-请选择-</option>
                <option value="user_id">ID</option>
                <option value="username">账号</option>
                <option value="nick_name">用户名</option>
                <option value="phone">手机号</option>
                <option value="email">邮箱</option>
                <option value="name">角色</option>
            </select>&emsp;
            <input id="edu-search-value" class="layui-input search-input" type="text" placeholder="输入关键字"/>&emsp;
            <button id="edu-btn-search" class="layui-btn icon-btn"><i class="layui-icon">&#xe615;</i>搜索</button>
            <button id="edu-btn-add" class="layui-btn icon-btn"><i class="layui-icon">&#xe654;</i>添加</button>
            <button id="edu-btn-addStudent" class="layui-btn icon-btn"><i class="layui-icon">&#xe654;</i>学生添加</button>
        </div>

        <table class="layui-table" id="edu-table" lay-filter="edu-table"></table>
    </div>
</div>


<!-- 表格操作列 -->
<script type="text/html" id="edu-table-bar">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-xs" lay-event="reset">重置密码</a>
</script>

<!-- 表格状态列 -->
<script type="text/html" id="edu-tpl-state">
    <input type="checkbox" lay-filter="edu-tpl-state" value="{{d.id}}" lay-skin="switch" lay-text="正常|锁定"
           {{d.enabled==true?'checked':''}}/>
</script>

<script>
    layui.use(['form', 'table', 'util', 'config', 'admin'], function () {
        var form = layui.form;
        var table = layui.table;
        var config = layui.config;
        var layer = layui.layer;
        var util = layui.util;
        var admin = layui.admin;


        // 渲染表格
        table.render({
            elem: '#edu-table',
            // url: config.base_server + 'user_query.json',
            url: config.base_server + 'api-user/personList',
            method: 'GET',
            where: {
                access_token: config.getToken().access_token
            },
            page: true,
            cols: [[
                {type: 'numbers'},
                {field: 'id', sort: true, title: '用户编号'},
                {field: 'username', sort: true, title: '账号'},
                {field: 'nickname', sort: true, title: '用户名'},
                {field: 'phone', sort: true, title: '手机号'},
                {field: 'email', sort: true, title: '邮箱'},
                {
                    sort: true, title: '性别', templet: function (d) {
                        return d.sex == 0 ? "男" : "女"
                    }
                },
                {
                    sort: true, templet: function (d) {
                        return util.toDateString(d.createTime);
                    }, title: '创建时间'
                },
                {
                    title: '角色', templet: function (res) {
                        let roles = [];
                        $.each(res.roles, function (key, val) {
                            roles.push(val.name);
                        });
                        return roles;
                    }
                },
                {field: 'enabled', sort: true, templet: '#edu-tpl-state', title: '状态', width: 150},
                {align: 'center', toolbar: '#edu-table-bar', title: '操作', width: 150}
            ]]
        });

        $('#edu-btn-add').click(function () {
            admin.popupCenter(
                {
                    title: "添加用户",
                    path: 'pages/system/educationalAdmin_add.html',
                    finish: function () {
                        table.reload('edu-table', {});
                    }
                }
            )
        });
        $('#edu-btn-addStudent').click(function () {
            admin.popupCenter(
                {
                    title: "添加学生",
                    path: 'pages/system/educationalAdmin_addStu.html',
                    finish: function () {
                        table.reload('edu-table', {});
                    }
                }
            )
        });
        // 搜索按钮点击事件
        $('#edu-btn-search').click(function () {
            var key = $('#edu-search-key').val();
            var value = $('#edu-search-value').val();
            table.reload('edu-table', {where: {searchKey: key, searchValue: value}});
        });

        //工具条点击事件
        table.on('tool(edu-table)', function (obj) {
            var data = obj.data;
            var layEvent = obj.event;

            if (layEvent === 'edit') { // 修改
                showEditModel(data);
            } else if (layEvent === 'reset') { // 重置密码
                layer.confirm('确定重置此用户的密码吗？', function (i) {
                    layer.close(i);
                    layer.load(2);
                    admin.req('api-user/users/' + obj.data.id + '/resetPassword', {}, function (data) {
                        layer.closeAll('loading');
                        if (data.code == 0) {
                            layer.msg(data.msg, {icon: 1, time: 500});
                        } else {
                            layer.msg(data.msg, {icon: 2, time: 500});
                        }
                    }, 'POST');
                });
            }
        });

        var showEditModel = function (data) {
            //修改sex值
            if (data) {
                data.sex = data.sex + '';
            }
            admin.putTempData('t_edu', data);
            if (data.name == "学生") {
                admin.popupCenter(
                    {
                        title: "修改学生信息",
                        path: 'pages/system/educationalAdmin_addStu.html',
                        finish: function () {
                            table.reload('edu-table', {});
                        }
                    }
                )

            } else {
                admin.popupCenter(
                    {
                        title: "修改用户",
                        path: 'pages/system/educationalAdmin_add.html',
                        finish: function () {
                            table.reload('edu-table', {});
                        }
                    }
                )

            }
        };


        // 修改user状态
        form.on('switch(edu-tpl-state)', function (obj) {
            layer.load(2);
            admin.req('api-user/users/updateEnabled', {
                id: obj.elem.value,
                enabled: obj.elem.checked ? true : false
            }, function (data) {
                layer.closeAll('loading');
                if (data.code == 0) {
                    layer.msg(data.msg, {icon: 1, time: 500});
                    //table.reload('table-user', {});
                } else {
                    layer.msg(data.msg, {icon: 2, time: 500});
                    $(obj.elem).prop('checked', !obj.elem.checked);
                    form.render('checkbox');
                }
            }, 'GET');
        });
    });
</script>